package com.wuliupai.delivery.wuliupaidelivery.activity;

import android.content.Intent;
import android.graphics.Color;
import android.support.v4.content.LocalBroadcastManager;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.wuliupai.delivery.wuliupaidelivery.R;
import com.wuliupai.delivery.wuliupaidelivery.bean.OrderBean;
import com.wuliupai.delivery.wuliupaidelivery.constants.BroadcastChannels;
import com.wuliupai.delivery.wuliupaidelivery.constants.InterfaceNet;
import com.wuliupai.delivery.wuliupaidelivery.net.NetResultOperate;
import com.wuliupai.delivery.wuliupaidelivery.utils.JsonUtils;
import com.wuliupai.delivery.wuliupaidelivery.utils.LogUtils;
import com.wuliupai.delivery.wuliupaidelivery.utils.SelectUtil;
import com.wuliupai.delivery.wuliupaidelivery.utils.StringUtils;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.Extra;
import org.androidannotations.annotations.ViewById;


@EActivity(R.layout.activity_grab_order_detail)
public class GrabOrderDetailActivity extends AppBaseActivity {

    // 出发
    @ViewById(R.id.rl_addressFrom)
    RelativeLayout rl_addressFrom;
    @ViewById(R.id.tv_addressFrom)
    TextView tv_addressFrom;
    @ViewById(R.id.tv_personFrom)
    TextView tv_personFrom;
    @ViewById(R.id.tv_personPhoneFrom)
    TextView tv_personPhoneFrom;

    // 目的
    @ViewById(R.id.tv_addressTo)
    TextView tv_addressTo;
    @ViewById(R.id.tv_personTo)
    TextView tv_personTo;
    @ViewById(R.id.tv_personPhoneTo)
    TextView tv_personPhoneTo;

    // 货物信息
    @ViewById(R.id.tv_orderNo)
    TextView tv_orderNo;
    @ViewById(R.id.tv_goodsName)
    TextView tv_goodsName;
    @ViewById(R.id.tv_goodsWeightVolume)
    TextView tv_goodsWeightVolume;
    @ViewById(R.id.tv_goodsRemarkLabel)
    TextView tv_goodsRemarkLabel;
    @ViewById(R.id.tv_goodsRemark)
    TextView tv_goodsRemark;


    // 支付信息
    @ViewById(R.id.tv_payType)
    TextView tv_payType;
    @ViewById(R.id.tv_payTotal)
    TextView tv_payTotal;
    @ViewById(R.id.tv_payAlready)
    TextView tv_payAlready;
    @ViewById(R.id.tv_payLeft)
    TextView tv_payLeft;

    @ViewById(R.id.btn_grabOrder)
    Button btn_grabOrder;

    @Extra("orderId")
    String orderId = "";
    @Extra("warehouseId")
    String warehouseId = "";
    @Extra("collectGoods")
    String collectGoods = ""; // 0-揽收，1-派送

    private GrabOrderDetailActivity _activity;

    private String phonePersonFrom = ""; // 起点电话
    private String phonePersonTo = ""; // 终点电话

    @AfterViews
    void onPageStart() {
        _activity = this;
        rl_addressFrom.setBackgroundColor(Color.WHITE);

        setDefaultTitle(_activity, R.string.string_grabOrderDetail_title);

        loadData();

        LogUtils.log("warehouseId---- "+warehouseId);
    }

    private void loadData() {
        OrderBean orderDetailBean = new OrderBean();
        orderDetailBean.setOrderId(orderId);

        String jsonOrderDetail = JsonUtils.createJsonString(orderDetailBean);
        LogUtils.log("请求数据~~~ " + jsonOrderDetail);
        try {
            String aesOrderDetail = JsonUtils.aes(jsonOrderDetail);
            callNet(this, InterfaceNet.Net_GetOrderDetailById, aesOrderDetail, new NetResultOperate() {
                @Override
                public void onSuccess(String content) {

                    if (!StringUtils.isEmpty(content)) {
                        Object obj = JsonUtils.parseJsonString(content, OrderBean.class);
                        if (null != obj) {
                            if (obj instanceof OrderBean) {

                                btn_grabOrder.setVisibility(View.VISIBLE);// 显示按钮

                                OrderBean bean = (OrderBean) obj;

                                tv_addressFrom.setText(bean.getStartAddress());
                                tv_personFrom.setText(bean.getConsignorName());
                                phonePersonFrom = bean.getConsignorMobile();
                                tv_personPhoneFrom.setText(phonePersonFrom);

                                tv_addressTo.setText(bean.getEndAddress());
                                tv_personTo.setText(bean.getConsigneeName());
                                phonePersonTo = bean.getConsigneeMobile();
                                tv_personPhoneTo.setText(phonePersonTo);

                                tv_orderNo.setText(bean.getOrderCode());
                                tv_goodsName.setText(bean.getGoodsName());
                                StringBuilder sb = new StringBuilder();
                                sb.append(StringUtils.formatWeight(bean.getGoodsWeight())).append(" / ")
                                        .append(StringUtils.formatVolume(bean.getGoodsVolume())).append(" / ")
                                        .append(bean.getGoodsCount()).append("件");
                                tv_goodsWeightVolume.setText(sb.toString());

                                String remark = bean.getRemarks();
                                tv_goodsRemarkLabel.setVisibility(View.VISIBLE);
                                tv_goodsRemark.setVisibility(View.VISIBLE);
                                tv_goodsRemark.setText(StringUtils.isEmpty(remark) ? "无备注信息" : remark);

                                tv_payType.setText(SelectUtil.initPayWay().get(bean.getPayType()));//支付方式

                                Integer dealPrice = Integer.valueOf(bean.getDealPrice());//运费总额
                                Integer actualMoney = Integer.valueOf(bean.getActualMoney());//已付金额

                                //支付详情展示
                                tv_payTotal.setText("￥" + (dealPrice / 100));
                                tv_payAlready.setText("- ￥" + (actualMoney / 100));
                                tv_payLeft.setText("= ￥" + (dealPrice - actualMoney) / 100);
                            }
                        }
                    }
                }

                @Override
                public void onDataEmpty(String content) {

                }

                @Override
                public void onDataEnd(String content) {

                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 点击抢单按钮
     */
    @Click(R.id.btn_grabOrder)
    void grabBtnClick() {
        OrderBean requestBean = new OrderBean();
        requestBean.setOrderId(orderId);
        requestBean.setWarehouseId(warehouseId);
        requestBean.setCollectGoods(collectGoods);

        String requestJson = JsonUtils.createJsonString(requestBean);
        LogUtils.log("warehouseId   "+warehouseId+"  请求数据~~~ "+requestJson);
        try {
            String requestAes = JsonUtils.aes(requestJson);
            _activity.callNet(_activity, InterfaceNet.Net_GrabOrder, requestAes, new NetResultOperate() {
                @Override
                public void onSuccess(String content) {
                    Intent intent = new Intent(BroadcastChannels.Channel_GrabOrderSuccess);
                    LocalBroadcastManager.getInstance(_activity).sendBroadcast(intent);
                    finish();
                }

                @Override
                public void onDataEmpty(String content) {
                }

                @Override
                public void onDataEnd(String content) {
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 拨打起点的电话
     */
    @Click(R.id.tv_personPhoneFrom)
    void onPersonPhoneFrom() {
        if (!StringUtils.isEmpty(phonePersonFrom)) {
            callPhone(phonePersonFrom);
        }
    }

    /**
     * 拨打终点的电话
     */
    @Click(R.id.tv_personPhoneTo)
    void onPersonPhoneTo() {
        if (!StringUtils.isEmpty(phonePersonTo)) {
            callPhone(phonePersonTo);
        }
    }
}
